<?php
/*
Plugin Name: Jon's Showoff Rankings Plugin
Plugin URI: http://www.jonlee.ca/jons-showoff-rankings/
Description: Provides an easy way to display your blog's Technorati ranking, Alexa ranking and Google PageRank. 
Version: 0.21
Author: Jon Lee
Author URI: http://www.jonlee.ca
*/

/*  Copyright 2007 Jon Lee (email : jon@jonlee.ca)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/


/* Custom PageRank - If your page rank isn't displaying properly, you can customize the value here since it doesn't change very often.
					 This isn't meant for you to "fake" your PageRank
					 Uncomment the line below and fill in your own PageRank (default is at 3) */
//$defined_pagerank = 3;


//Thank the author (me) for this plug-in by including a link to plug-in page. If you change this you'll need to reload the cache (and I'll be sad).
$jsr_thank_author = true;

//Array of default settings
$jsr_defaults = array(
    'jsr_siteurl'				=> "",					/* URL of the site to display stats for */
    'jsr_cache_time'			=> 60 * 60,				/* How often to update rankings/How long to keep data in cache for (in seconds) */
    'jsr_cache_file'			=> "jsr_cache.html",	/* the cache file to save data to */
	'jsr_vertical'				=> "0",					/* Display rankings vertically or horizontally */
	
	'jsr_technorati_use'		=> "",					/* Whether or not to display technorati ranking */
    'jsr_technorati_apikey'		=> "",					/* Technorati API key, get from here: http://technorati.com/developers/apikey.html */
	'jsr_technorati_graphical'	=> "",					/* Use graphical or text technorati rank */
	
	'jsr_pagerank_use'			=> "",					/* Whether or not to use pagerank */
	'jsr_pagerank_graphical' 	=> "",					/* Use graphical or text page rank */
	
	'jsr_alexa_use'				=> "",					/* Whether or not ot use Alexa rank */
	'jsr_alexa_graphical'		=> "",					/* Use graphical or text Alexa rank */
	
    'jsr_version'				=> "0.21"				/* version number */
);

//Get current options
$jsr_options = get_option('showoff_ranking_options');

//Check if there are no options, if not set options to default options
if(empty($jsr_options)) {
	add_option('showoff_ranking_options', $jsr_defaults, 'Jon\'s Showoff Rankings Plugin Settings');
} elseif($jsr_options['jsr_version'] < $jsr_defaults['jsr_version'])	//check plugin version, update if necessary
{	$jsr_options['jsr_version'] = $jsr_defaults['jsr_version'];
	update_option('showoff_ranking_options', $jsr_options);
}

//Add options page to the admin menu
add_action('admin_menu', 'showoff_rankings_options_page');

//Add widget to sidebar widgets
add_action('widgets_init', 'jsr_widget_init');

//Add css to the header
add_filter('wp_head', 'jsr_css');


/**
 * showoff_rankings_options_page:
 * Adds options page to the admin panel
 */
function showoff_rankings_options_page()
{	if(function_exists('add_options_page')) {
	add_options_page('Jon\'s Showoff Rankings Plugin', 'Showoff Rankings', 8, basename(__FILE__), 'showoff_rankings_options_panel'); }
}


/**
 * showoff_rankings_options_panel:
 * The actual page to add to the admin panel
 */
function showoff_rankings_options_panel()
{	global $jsr_options, $_POST;

	//set debug mode (keep off)
	$debug = false;
	
	/* Handle settings change */
	$jsr_flash = false;
	if(isset($_POST['jsr_save']))
	{	
		$jsr_options['jsr_siteurl'] 				= $_POST['jsr_siteurl'];
		$jsr_options['jsr_cache_time'] 				= $_POST['jsr_cache_time'];
		$jsr_options['jsr_vertical'] 				= $_POST['jsr_vertical'];
	
		$jsr_options['jsr_technorati_use'] 			= $_POST['jsr_technorati_use'];
		$jsr_options['jsr_technorati_apikey'] 		= $_POST['jsr_technorati_apikey'];
		$jsr_options['jsr_technorati_graphical'] 	= $_POST['jsr_technorati_graphical'];
		
		$jsr_options['jsr_pagerank_use'] 			= $_POST['jsr_pagerank_use'];
		$jsr_options['jsr_pagerank_graphical'] 		= $_POST['jsr_pagerank_graphical'];
		
		$jsr_options['jsr_alexa_use'] 				= $_POST['jsr_alexa_use'];
		$jsr_options['jsr_alexa_graphical'] 		= $_POST['jsr_alexa_graphical'];

		update_option('showoff_ranking_options', $jsr_options);
		
		jsr_display_rankings(true);
		echo '<div id="message" class="updated fade"><p>Settings saved. Cache updated.</p></div>';
    } elseif (isset($_POST['jsr_rebuildcache']))
	{	jsr_display_rankings(true);
		echo '<div id="message" class="updated fade"><p>Cache updated</p></div>';
	}

	
	/* Options page constructor */
	
	// auto-update script
	?>
	<script src="http://www.jonlee.ca/checkversion.php?plugin=showoffrankings&amp;v=<?php echo $jsr_options['jsr_version'];?>&amp;referrer=<?php bloginfo('blog_url');?>" type="text/javascript"></script>
		  
	<div class="wrap">
		<h2>Jon's Showoff Rankings Options</h2>
		
		<form action="options-general.php?page=jons_showoff_rankings.php" method="post">
			<fieldset class="options">
				<legend>General Settings</legend>
				<table class="optiontable">
					<tr valign="top">
						<th scope="row">
							<label for="jsr_siteurl">Your site URL</label>
						</th>
						<td>
							<input name="jsr_siteurl" type="text" size="50" value="<?php echo $jsr_options['jsr_siteurl']; ?>" />
							<br />
							Enter the site URL you wish to display stats for.  For almost all purposes, the site URL should be the root of your blog. i.e. http://www.jonlee.ca
						</td>
					</tr>		
					<tr valign="top">
						<th scope="row">
							<label for="jsr_cache_time">Ranking update Interval</label>
						</th>
						<td>
							<input type="radio" name="jsr_cache_time" value="3600" title="Every hour" <?php if ($jsr_options['jsr_cache_time'] == 3600 ) { echo 'checked '; } ?>/> Every hour<br />
							<input type="radio" name="jsr_cache_time" value="21600" title="Every 6 hours" <?php if ($jsr_options['jsr_cache_time'] == 21600 ) { echo 'checked '; } ?>/> Every 6 hours<br />
							<input type="radio" name="jsr_cache_time" value="43200" title="Every 12 hours" <?php if ($jsr_options['jsr_cache_time'] == 43200 ) { echo 'checked '; } ?>/> Every 12 hours<br />
							<input type="radio" name="jsr_cache_time" value="86400" title="Every day" <?php if ($jsr_options['jsr_cache_time'] == 86400 ) { echo 'checked '; } ?>/> Every day<br />
							<input type="radio" name="jsr_cache_time" value="172800" title="Every 2 days" <?php if ($jsr_options['jsr_cache_time'] == 172800 ) { echo 'checked '; } ?>/> Every 2 days<br />
							<em>Note: PageRank is updated 4 times a year; Alexa and Technorati is updated once every few days.</em>
						</td>
					</tr>
					<tr valign="top">
						<th scope="row"><label for="jsr_vertical">Display Rankings Vertically</label></th>
						<td>
						    <input name="jsr_vertical" type="checkbox" value="1"<?php checked('1', $jsr_options['jsr_vertical']);?> disabled="disabled" />
							Coming soon...
						</td>
					</tr>
				</table>
			</fieldset>
			<fieldset class="options">
				<legend>PageRank Settings</legend>
				<table class="optiontable">
					<tr valign="top">
						<th scope="row"><label for="jsr_pagerank_use">Display Google PageRank</label></th>
						<td>
						    <input name="jsr_pagerank_use" type="checkbox" value="1"<?php checked('1', $jsr_options['jsr_pagerank_use']);?> />
							Select whether or not you wish to display your Google PageRank.
						</td>
					</tr>
					<tr valign="top">
						<th scope="row"><label for="jsr_pagerank_graphical">Display Style</label></th>
						<td>
							<input name="jsr_pagerank_graphical" type="radio" value="0"<?php checked('0', $jsr_options['jsr_pagerank_graphical']);?> /> Text &nbsp; 
							<input name="jsr_pagerank_graphical" type="radio" value="1"<?php checked('1', $jsr_options['jsr_pagerank_graphical']);?> /> Graphic &nbsp; 
							<input name="jsr_pagerank_graphical" type="radio" value="2"<?php checked('2', $jsr_options['jsr_pagerank_graphical']);?> /> Both
						</td>
					</tr>
				</table>
			</fieldset>
			<fieldset class="options">
				<legend>Alexa Settings</legend>
				<table class="optiontable">
					<tr valign="top">
						<th scope="row"><label for="jsr_alexa_use">Display Alexa Ranking</label></th>
						<td>
						    <input name="jsr_alexa_use" type="checkbox" value="1"<?php checked('1', $jsr_options['jsr_alexa_use']);?> />
							Select whether or not you wish to display an Alexa ranking.
						</td>
					</tr>
					<tr valign="top">
						<th scope="row"><label for="jsr_alexa_graphical">Display Style</label></th>
						<td>
							<input name="jsr_alexa_graphical" type="radio" value="0"<?php checked('0', $jsr_options['jsr_alexa_graphical']);?> /> Text &nbsp; 
							<input name="jsr_alexa_graphical" type="radio" value="1"<?php checked('1', $jsr_options['jsr_alexa_graphical']);?> /> Graphic &nbsp; 
							<input name="jsr_alexa_graphical" type="radio" value="2"<?php checked('2', $jsr_options['jsr_alexa_graphical']);?> /> Both
						</td>
					</tr>
				</table>
			</fieldset>
			<fieldset class="options">
				<legend>Technorati Settings</legend>
				<table class="optiontable">
					<tr valign="top">
						<th scope="row"><label for="jsr_technorati_use">Display Technorati Ranking</label></th>
						<td>
							<input name="jsr_technorati_use" type="checkbox" value="1"<?php checked('1', $jsr_options['jsr_technorati_use']);?> />
							Select whether or not you wish to display your Technorati ranking.
						</td>
					</tr>
					<tr valign="top">
						<th scope="row"><label for="jsr_technorati_apikey">Technoarti API Key</label></th>
						<td>
							<input name="jsr_technorati_apikey" type="text" size="50" maxlength="50" value="<?php echo $jsr_options['jsr_technorati_apikey']; ?>" /><br />
							In order to display your Technorati ranking, you must be registered with Technorati and you'll need to supply your API key.  Your key can be found by <a href="http://technorati.com/developers/apikey.html" target="_blank" title="Get API Key">clicking this link</a>.
						</td>
					</tr>
					<tr valign="top">
						<th scope="row"><label for="jsr_pagerank_graphical">Display Style</label></th>
						<td>
							<input name="jsr_technorati_graphical" type="radio" value="0"<?php checked('0', $jsr_options['jsr_technorati_graphical']);?> /> Text &nbsp; 
							<input name="jsr_technorati_graphical" type="radio" value="1"<?php checked('1', $jsr_options['jsr_technorati_graphical']);?> /> Graphic &nbsp; 
							<input name="jsr_technorati_graphical" type="radio" value="2"<?php checked('2', $jsr_options['jsr_technorati_graphical']);?> /> Both
						</td>
					</tr>

				</table>
			</fieldset>
			<p class="submit">
				<input type="hidden" name="jsr_save" value="1" />
				<input type="submit" value="Save Options and Rebuild Cache" />
			</p>
		</form>
		<form action="options-general.php?page=jons_showoff_rankings.php" method="post">
			<p class="submit">
				<input type="hidden" name="jsr_rebuildcache" value="1" />
				<input type="submit" value="Rebuild Cache (Manually Update Rankings)" />
			</p>
		</form>
		<?php
		if ($debug)
		{	$data = print_r($jsr_options, true);
			$data = str_replace( " ","&nbsp;", $data);
			$data = str_replace( "\r\n","<br>\r\n", $data);
			$data = str_replace( "\r","<br>\r", $data);
			$data = str_replace( "\n","<br>\n", $data);
		
			echo $data;
		} ?>
		<h2>Installation</h2>
		<p style="font-size:14px;">After selecting the desired options below, add the following code to where you wish the rankings to be displayed on your page:</p>
		<blockquote>&lt;?php if (function_exists('jsr_display_rankings')) { jsr_display_rankings(); } ?&gt;</blockquote>
		<p style="font-size:14px;">If you have Sidebar Widgets activated, you can use that instead.</p>
		<p style="font-size:14px;">Questions? Comments? Suggestions? Troubleshooting? <a href="http://www.jonlee.ca/jons-showoff-rankings/">Check out the plug-in homepage.</a></p>
	</div>
	<?php
} //end admin menu function 


function jsr_display_rankings($force_recache=false)
{	global $jsr_options, $jsr_thank_author;

	$cachefile = ABSPATH . "wp-content/plugins/jons_showoff_rankings/" . $jsr_options['jsr_cache_file'];

	if ($jsr_options['jsr_siteurl'] != "")
	{		
		//check if the cache file exists and is still 'up-to-date'
		if (file_exists($cachefile) && (time() - $jsr_options['jsr_cache_time'] < filemtime($cachefile)) && $force_recache == false)
		{	include($cachefile);
			echo "<!-- Cached: " . date("Y-m-d H:i:s", filemtime($cachefile)) . " -->\n";
		}
		else {
			
			$content = "<div id=\"jsr_wrapper\">";
			
			/* You may rearrange the 3 IF statements below to change order of rankings */
			if (isset($jsr_options['jsr_pagerank_use']))	{	$content .= jsr_get_pagerank();			}
			if (isset($jsr_options['jsr_alexa_use']))		{	$content .= jsr_get_alexa_rank();		}
			if (isset($jsr_options['jsr_technorati_use']))	{	$content .= jsr_get_technorati_rank();	}
			/* End arrange order */

			
			if ($jsr_thank_author) {
			//Begin link to author page
			$content .= "<br /><div id=\"jsr_about\">";
			$content .= "<a href=\"http://www.jonlee.ca/jons-showoff-rankings/\" title=\"Visit Plug-in Homepage - Jon Lee Web Development\">[about plug-in]</a>";
			$content .= "</div>";
			//End link to author page
			}
			
			$content .= "</div>"; //close wrapper	
			
			
			$fp = fopen($cachefile, 'w') or die("Cannot open cache file for reading");
			fwrite($fp, $content);
			fclose($fp);
			
			if ($force_recache == false)
			{	echo $content;
			}
		}
	}
	else {
		echo "Error - Did you fill in your site URL in the options?";
	}
}


function jsr_get_technorati_rank()
{	global $jsr_options;
	
	//The following code is a modification of code by Bobby Matthis (ReformatThis.com) created for Saman Sadeghi (Samanathon.com)
	//It is based off the idea/plugin of Doug Karr (DouglasKarr.com)
	
	$technorati_xml = "http://api.technorati.com/bloginfo?key=" . $jsr_options['jsr_technorati_apikey'] . "&url=" . $jsr_options['jsr_siteurl'];
	$xml_parser = xml_parser_create();
	$fp = fopen($technorati_xml, "r") or die("Error: Reading XML data.");
	$data = "";
	while (!feof($fp)) {
		$data .= fread($fp, 8192);
	}
	fclose($fp);
	xml_parse_into_struct($xml_parser, $data, $vals, $index);
	xml_parser_free($xml_parser);
	
	//get values
	$index_rank = $index['RANK'][0];
	$techno_rank = number_format(trim($vals[$index_rank]['value']));
	$index_url = $index['URL'][0];
	$techno_url = trim($vals[$index_url]['value']);
	$index_inblogs = $index['INBOUNDBLOGS'][0];
	$techno_inblogs = number_format(trim($vals[$index_inblogs]['value']));
	$index_inlinks = $index['INBOUNDLINKS'][0];
	$techno_inlinks = number_format(trim($vals[$index_inlinks]['value']));
	$index_update = $index['LASTUPDATE'][0];
	$techno_update = trim($vals[$index_update]['value']);
	
	$rank = 38;
	if ($index_rank > 0) {
		$rank = log($index_rank) / log(5);
		$rank = floor($rank);
		if ($rank > 10) {
			$rank = 10;
		}
	}
	$width = ((10 - $rank) << 2);
		
	$output = '<span class="jsr_rank_container"><a href="http://www.technorati.com/blogs/'.$jsr_options['jsr_siteurl'].'" title="Technorati rank: '.$techno_rank.' ('.$techno_inlinks.' links from '.$techno_inblogs.' blogs)">Technorati';
	if ($jsr_options['jsr_technorati_graphical'] == 1 || $jsr_options['jsr_technorati_graphical'] == 2) { //text
		$output .= '	<span class="jsr_rank_bar"><span class="jsr_rank_border"><span class="jsr_rank_bar_filled" style="width: '.$width.'px; background-color: #4cbc00; border-bottom: 1px solid #5eaa5e;"></span></span></span>';
	}
	if ($jsr_options['jsr_technorati_graphical'] == 0 || $jsr_options['jsr_technorati_graphical'] == 2) { //graphical
		$output .= '	<span class="jsr_rank_text" style="color:#4cbc00">'.$techno_rank.'</span>';
	}
	$output .= '</a></span>';
	
	return $output;
}


function jsr_get_pagerank()
{	global $jsr_options, $defined_pagerank;

	if (empty($defined_pagerank)) // if pagerank not defined
	{

		define('GOOGLE_MAGIC', 0xE6359A60);
		
		//Yea this is really dirty and I'm lazy but it works!
		class pageRank{
		var $pr; 
		 function zeroFill($a, $b){
		 $z = hexdec(80000000);
		  if ($z & $a){
		   $a = ($a>>1);
		   $a &= (~$z);
		   $a |= 0x40000000;
		   $a = ($a>>($b-1));
		  }else{
		   $a = ($a>>$b);
		  }
		 return $a;
		 } 
		 
		 function mix($a,$b,$c) {
		   $a -= $b; $a -= $c; $a ^= ($this->zeroFill($c,13));
		   $b -= $c; $b -= $a; $b ^= ($a<<8);
		   $c -= $a; $c -= $b; $c ^= ($this->zeroFill($b,13));
		   $a -= $b; $a -= $c; $a ^= ($this->zeroFill($c,12));
		   $b -= $c; $b -= $a; $b ^= ($a<<16);
		   $c -= $a; $c -= $b; $c ^= ($this->zeroFill($b,5));
		   $a -= $b; $a -= $c; $a ^= ($this->zeroFill($c,3));
		   $b -= $c; $b -= $a; $b ^= ($a<<10);
		   $c -= $a; $c -= $b; $c ^= ($this->zeroFill($b,15));
		   return array($a,$b,$c);
		 }
		 
		 function GoogleCH($url, $length=null, $init=GOOGLE_MAGIC) {
		  if(is_null($length)) {
		   $length = sizeof($url);
		  }
		  $a = $b = 0x9E3779B9;
		  $c = $init;
		  $k = 0;
		  $len = $length;
		  while($len >= 12) {
		   $a += ($url[$k+0] +($url[$k+1]<<8) +($url[$k+2]<<16) +($url[$k+3]<<24));
		   $b += ($url[$k+4] +($url[$k+5]<<8) +($url[$k+6]<<16) +($url[$k+7]<<24));
		   $c += ($url[$k+8] +($url[$k+9]<<8) +($url[$k+10]<<16)+($url[$k+11]<<24));
		   $mix = $this->mix($a,$b,$c);
		   $a = $mix[0]; $b = $mix[1]; $c = $mix[2];
		   $k += 12;
		   $len -= 12;
		  }
		  $c += $length;
		  switch($len){
		   case 11: $c+=($url[$k+10]<<24);
		   case 10: $c+=($url[$k+9]<<16);
		   case 9 : $c+=($url[$k+8]<<8);
		   /* the first byte of c is reserved for the length */
		   case 8 : $b+=($url[$k+7]<<24);
		   case 7 : $b+=($url[$k+6]<<16);
		   case 6 : $b+=($url[$k+5]<<8);
		   case 5 : $b+=($url[$k+4]);
		   case 4 : $a+=($url[$k+3]<<24);
		   case 3 : $a+=($url[$k+2]<<16);
		   case 2 : $a+=($url[$k+1]<<8);
		   case 1 : $a+=($url[$k+0]);
		  }
		  $mix = $this->mix($a,$b,$c);
		 /* report the result */
		 return $mix[2];
		 }
		 
		 //converts a string into an array of integers containing the numeric value of the char
		 
		 function strord($string) {
		  for($i=0;$i<strlen($string);$i++) {
		   $result[$i] = ord($string{$i});
		  }
		 return $result;
		 }
		 
		 function printrank($url){
		  $ch = "6".$this->GoogleCH($this->strord("info:" . $url));
		  
		  $fp = fsockopen("www.google.com", 80, $errno, $errstr, 30);
		  if (!$fp) {
			 echo "$errstr ($errno)<br />\n";
		  } else {
			 $out = "GET /search?client=navclient-auto&ch=" . $ch .  "&features=Rank&q=info:" . $url . " HTTP/1.1\r\n" ;
			 $out .= "Host: www.google.com\r\n" ;
			 $out .= "Connection: Close\r\n\r\n" ; 
			 fwrite($fp, $out);
			 while (!feof($fp)) {
			   $data = fgets($fp, 128);
			   $pos = strpos($data, "Rank_");
				 if($pos === false){
				 }else{
				   $pagerank = substr($data, $pos + 9);
				   $this->pr = $pagerank;
				 }
			 }
			 fclose($fp);
		  }
		 }
		
		 function get_pr(){
		  return $this->pr;
		 }
		}
		$gpr = new pageRank();
		$gpr->printrank($jsr_options['jsr_siteurl']);
		$pagerank = $gpr->get_pr();
	} else { //defined pagerank
		$pagerank = $defined_pagerank;
	}
	
	$width = $pagerank*4;
	
	$output = '<span class="jsr_rank_container"><a href="#" title="PageRank: '.$pagerank.'/10">PageRank';
	if ($jsr_options['jsr_pagerank_graphical'] == 1 || $jsr_options['jsr_pagerank_graphical'] == 2) { //text
		$output .= '	<span class="jsr_rank_bar"><span class="jsr_rank_border"><span class="jsr_rank_bar_filled" style="width: '.$width.'px; background-color: #38a600; border-bottom: 1px solid #329301;"></span></span></span>';
	}
	if ($jsr_options['jsr_pagerank_graphical'] == 0 || $jsr_options['jsr_pagerank_graphical'] == 2) { //graphical
		$output .= '	<span class="jsr_rank_text" style="color:#38a600">'.$pagerank.'/10</span>';
	}
	
	$output .= '</a></span>';
	
	return $output;
}

function jsr_get_alexa_rank()
{	global $jsr_options;

	/* old way - incompatible with php5 
	$xmlDoc = domxml_open_file("http://xml.alexa.com/data?cli=10&dat=nsa&url=".$jsr_options['jsr_siteurl']);
	$xmlArray = $xmlDoc->get_elements_by_tagname('POPULARITY');

	$alexarank = $xmlArray[0]->get_attribute('TEXT'); */

	$alexa_url = "http://xml.alexa.com/data?cli=10&dat=nsa&url=".$jsr_options['jsr_siteurl'];
	$fp = fopen($alexa_url, "r") or die("Error: Reading Alexa XML data.");
	$data = "";
	while (!feof($fp)) {
		$data .= fread($fp, 8192);
	}
	preg_match ('/<POPULARITY URL=([^`]*?)\/>/', $data, $matches);
	preg_match ('/" TEXT="([^`]*?)"/', $matches[1], $rank);
	$alexarank = $rank[1];
	
	//from CoolCode.CN
	$arank = 38;
	if ($alexarank > 0) {
		$arank = log($alexarank) / log(5);
		$arank = floor($arank);
		if ($arank > 10) {
			$arank = 10;
		}
	}
	$width = ((10 - $arank) << 2);
	
	$output = '<span class="jsr_rank_container"><a href="http://www.alexa.com/data/details/main?url='.$jsr_options['jsr_siteurl'].'" title="Alexa rank: '.number_format($alexarank).'">Alexa';
	if ($jsr_options['jsr_alexa_graphical'] == 1 || $jsr_options['jsr_alexa_graphical'] == 2) { //text
		$output .= '	<span class="jsr_rank_bar"><span class="jsr_rank_border"><span class="jsr_rank_bar_filled" style="width: '.$width.'px; background-color: #4351B7; border-bottom: 1px solid #1B13B7;"></span></span></span>';
	}
	if ($jsr_options['jsr_alexa_graphical'] == 0 || $jsr_options['jsr_alexa_graphical'] == 2) { //graphical
		$output .= '	<span class="jsr_rank_text" style="color:#4351B7">'.number_format($alexarank).'</span>';
	}
	$output .= '</a></span>';
	
	return $output;
}

//css for ranking graphics
function jsr_css()
{
global $jsr_options;

if (isset($jsr_options['jsr_vertical'])) {
	$height = "100px";
	$clear = "left";
} else {
	$height = "35px";
	$clear = "none";
}

echo "
<style type=\"text/css\">
<!--
#jsr_wrapper {
	height: $height;
	font-family:Arial, Helvetica, sans-serif;
    font-size: 9px;
	font-weight:lighter;
	margin:10px 0 0 0;
	display:block;
	line-height:1.0em;
}
#jsr_wrapper a, #jsr_wrapper a:hover, #jsr_wrapper a:visited {
	text-decoration:none;
	font-size:9px;
	color:#000;
	font-weight:lighter;
	background:none;
	border: 0;
}
#jsr_about {
	font-size:8px;
	clear:none;
	display:block;
	margin-left:5px;
	margin-top:25px;
	width:100px;
}
.jsr_rank_container {
	clear: $clear;
    width: 42px;
	height: 30px;
	display: block;
    text-align: center;
	float: left;
	margin-left: 3px;
}
.jsr_rank_bar {
	width:42px;
    border: 0;
    padding: 0;
    margin: 0;
    font-size: 0;
    display: block;
}
.jsr_rank_border {
	width:40px;
	border: 1px solid #999999;
    padding: 0;
    margin: 0;
    text-align: left;
    display: block;
}
.jsr_rank_bar_filled {
    height: 3px;
    padding: 0;
    margin: 0;
    border: 0;
    display: block;
}
.jsr_rank_text {
	line-height:1.3em;
}
-->
</style>
";
} //end css function

//sidebar widget stuff
function jsr_widget_init() {

	//check if sidebar widgets is turned on
	if ( !function_exists(register_sidebar_widget) || !function_exists('register_widget_control') ){ return; }
	
	function jsr_widget($args) {
		extract($args);
		
        $options = get_option('widget_jsr');
        $title = $options['title'];
		
		echo $before_widget;
		echo $before_title . $title . $after_title;
		
		if (function_exists('jsr_display_rankings')) { jsr_display_rankings(); }
		
		echo $after_widget;
	}
	
	function jsr_widget_control()
	{
		$options = $newoptions = get_option('widget_jsr');

		if ( $_POST['jsr-widget-title'] ) {
				$newoptions['title'] = strip_tags(stripslashes($_POST['jsr-widget-title']));
		}

		if ( $options != $newoptions ) {
				$options = $newoptions;
				update_option('widget_jsr', $options);
		}

		?>
		<p><label for="jsr-widget-title">Title: <input type="text" style="width: 250px;" id="jsr-widget-title" name="jsr-widget-title" value="<?php echo htmlspecialchars($options['title']); ?>" /></label></p>
		<input type="hidden" name="jsr-widget-submit" id="jsr-widget-submit" value="1" />
	<?
	}
	register_sidebar_widget('Jon\'s Showoff Rankings', 'jsr_widget');
	register_widget_control('Jon\'s Showoff Rankings', 'jsr_widget_control');
}

?>